$(function () {

    //页面加载完成获取第一页数据
    getArticleList(1);
    $("body").on('click', '#btn1,#btn2', function () {
        //点击搜索查询从第一页开始查询
        getArticleList(1);
    });

    //单个发布
    $('body').on('click', ".releaseStatus", function () {
        var obj = $(this).parents('tr');
        var articleId = obj.data('id');
        icModale(2, "确认发布选中的资讯？", 3, function (){
            releaseArticleByIds(articleId, obj, 2);
        });
    });
    //批量发布
    $('body').on('click', "#releaseBatch", function () {
        var obj = $("input[name=articleId]:checked").parents('tr');
        var articleIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(articleIds)) {
            icModale(2, "确认批量发布选中的资讯？", 3, function () {
                releaseArticleByIds(articleIds, obj, 2);
            });
        }
    });
    //批量删除
    $("body").on("click", "#deleteBatch", function () {
        var obj = $("input[name = articleId]:checked").parents('tr');
        var articleIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(articleIds)) {
            icModale(2, "删除后不可恢复，仍然确认删除么？", 3, function () {
                updateArticleStatusByIds(articleIds, obj, 2);
                icModale(0, '成功批量删除选中资讯');
            });
        }
    });
    //批量隐藏
    $('body').on('click', "#lockBatch", function () {
        var obj = $("input[name='articleId']:checked").parents('tr');
        var articleIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(articleIds)) {
            icModale(2, "确认批量隐藏选中资讯？", 3, function (){
                updateArticleStatusByIds(articleIds, obj, 1);
                icModale(0, '成功批量隐藏选中资讯');
            });
        }
    });
       //单个删除
    $("body").on("click", ".deleteArticle", function () {
        var articleIds = $(this).data('id');
        var obj = $(this);
        icModale(2, "删除后不可恢复，仍然确认删除么？", 3, function () {
            updateArticleStatusByIds(articleIds, obj,2);
            icModale(0, '成功删除选中资讯');
        });
    });
    //单个显示/隐藏
    $('body').on('click', '.unlockAndHistory', function () {
        var articleIds = $(this).data('id');
        var status = $(this).data('status');
        var obj = $(this).parents('tr');
            if (status == 0) {
                icModale(2, "确认隐藏选中资讯？", 3, function () {
                    updateArticleStatusByIds(articleIds, obj, 1);
                    icModale(0, '成功隐藏选中资讯');
                });
            }
            if (status == 1) {
                icModale(2, "确认显示选中资讯？", 3, function() {
                    updateArticleStatusByIds(articleIds, obj, 0);
                    icModale(0, '成功显示选中资讯');
                });
            }
    });


    //批量恢复
    $('body').on('click', "#regainBatch", function () {
        var obj = $("input[name='articleId']:checked").parents('tr');
        var articleIds = obtainIds(obj);//获取勾选ids
            if (!$.isEmptyObject(articleIds)) {
                icModale(2, "确认批量恢复选中资讯？", 3, function (){
                updateArticleStatusByIds(articleIds, obj, 0);
                icModale(0, '成功恢复选中资讯');
            });
        }

    });


    //拼接id串
    function obtainIds(obj) {
        if(null == obj || obj.size() == 0){
            icModale(0,"请选择要批量操作的数据");
            return null;
        }
        var articleIds = "";
        $(obj).each(function (index,_input) {
            articleIds += $(_input).data('id') + ",";
        })
        articleIds = articleIds.substring(0,articleIds.length-1);
        return articleIds;
    }

    /**
     * 发布资讯
     * @param obj
     */
    function releaseArticleByIds(articleIds, obj) {
        $.ajax({
            url: baseloaction + "/admin/op/article/batchPublishArticle",
            data: {"articleIds": articleIds},
            type: "post",
            dataType: "json",
            success: function (result) {
                if (result.success) {
                    obj.find('.releaseStatus').next('div').remove();
                    obj.find('.releaseStatus').remove();
                    icModale(0, result.message);
                } else {
                    icModale(0, result.message);
                }
            }
        })
    }

    /**
     * 批量封禁，批量恢复，批量删除
     * @param userIds
     * @param obj
     * @param status
     */
    function updateArticleStatusByIds(articleIds, obj, status) {
        $.ajax({
            url: baseloaction + "/admin/op/article/updateArticleStatusByIds",
            data: {"articleIds": articleIds, "status": status},
            type: "post",
            dataType: "json",
            success: function (result) {
                if (result.success) {
                    if (1 == status) {
                        obj.find('.status').html('');
                        obj.find('.status').append('<span class="label label-danger">隐藏</span>');
                        obj.find(".unlockAndHistory").html("");
                        obj.find(".unlockAndHistory").data('status', '1');
                        obj.find(".unlockAndHistory").append('<i class="fa fa-history"></i>');
                        obj.find(".unlockAndHistory").attr('data-original-title',"恢复")
                    }
                    if (0 == status) {
                        obj.find('.status').html('');
                        obj.find('.status').append('<span class="label label-info">正常</span>');
                        obj.find(".unlockAndHistory").html("");
                        obj.find(".unlockAndHistory").data('status', '0');
                        obj.find(".unlockAndHistory").append('<i class="fa fa-unlock-alt"></i>');
                        obj.find(".unlockAndHistory").attr('data-original-title',"隐藏")
                    }
                    if (2 == status) {
                        getArticleList(pageNum);
                    }
                } else {
                    icModale(0, result.message);
                }
            },
            error: function (error) {
                icModale(0, "系统繁忙,请稍后重试");
            }
        })
    }

    /**
     * 获取资讯表页面
     * @param pageNo 当前页码
     */
    function getArticleList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/article/queryAjaxArticleList',//请求URL
            contextId: 'article-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getArticleList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-article', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#articleCount").html(total);
            }
        };
        ajaxGetListData(obj);
    }
});
